﻿/*
20101026 release memory added, if parent is DisplayObject AUTO release when the Object Event.REMOVED_FORM_STAGE;


Usage:
(1)
cache.address('bgsound', cacheSound).init(this);
(2)
var _MySound:cacheSound=new cacheSound(this);	// bring the parent object [DisplayObject] to addEvent Auto Release.
_MySound.preload('./sound/music.mp3','bgsound');
_MySound.preload('./sound/effect.mp3','boomeffect');
_MySound.preload('./sound/talk.mp3','CharTalk001');

_MySound.child('bgsound').loop=true;	// SoundFX Class.
_MySound.child('bgsound').autoplay=true;
_MySound.child('bgsound').play();

this.mute = true; // can handle all soundFX to Mute sound.
*/
package com.cf.core.Utils{
	import com.cf.core.media.SoundFX;
	import flash.media.Sound;
	import flash.media.SoundMixer;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import com.cf.core.events.LoadEvent;
	public class cacheSound extends EventDispatcher{
		private var __soundArr:Array=new Array;
		private var __mute:Boolean;
		private var G_parent:*;
		public function set mute(_t:Boolean):void{__mute=_t; allMute();}
		public function get mute():*{return __mute}
		
		public function cacheSound(Tparent:*=null):void{
			if( Tparent!=null ) init(Tparent);
		}//fn
		public function init(Tparent:*):void{
			G_parent=Tparent;
			G_parent.addEventListener(Event.REMOVED_FROM_STAGE, RELEASE);
		}//fn
		public function preload(_d:String,_uri:String):void{
			// want to preload & save in memory.
			var _i:int=__soundArr.length;
			if(__soundArr[_i]==undefined){
				__soundArr[_i] = new SoundFX;
				__soundArr[_i].id = _d;
				__soundArr[_i].url = _uri;
				__soundArr[_i].addEventListener(LoadEvent.ONPROGRESS, handleSound);
				__soundArr[_i].addEventListener(LoadEvent.ONLOAD, handleSound);
				__soundArr[_i].load(_uri);
			}//if
		}//fn
		
		public function child(_addr:String):SoundFX{
			for(var i:int=0;i<__soundArr.length;i++){
				if(__soundArr[i].id==_addr) return __soundArr[i];
			}//for
			var abc = new SoundFX();
			return abc;
		}//fn
		
		private function allMute():void{
			for(var i:int=0;i<__soundArr.length;i++){
				__soundArr[i].mute=mute;
			}//for
		}//fn
		public function stop():void{
			for(var i:int=0;i<__soundArr.length;i++){
				__soundArr[i].stop();
			}//for
		}//fn
		private function handleSound(e:LoadEvent):void{
			switch(e.type){
				case LoadEvent.ONPROGRESS:
					//trace(e.data.id +" : "+ e.data.bytesLoaded +" / "+ e.data.bytesTotal);
					dispatchEvent(new LoadEvent(LoadEvent.ONPROGRESS, e.data) );
				break;
				case LoadEvent.ONLOAD:
					//trace(e.data.id +" ["+e.data.url +"] Loaded");
					dispatchEvent(new LoadEvent(LoadEvent.ONLOAD, e.data) );
				break;
			}//case
		}//fn
		
		public function RELEASE(e:*=null):void{
			for(var i:int=0;i<__soundArr.length;i++){
				try{
					__soundArr[i].removeEventListener(LoadEvent.ONPROGRESS, handleSound);
					__soundArr[i].removeEventListener(LoadEvent.ONLOAD, handleSound);
					__soundArr[i].RELEASE();
				}catch(e){}
			}//for
			G_parent.removeEventListener(Event.REMOVED_FROM_STAGE, RELEASE);
		}//fn
	}//class
}//package